from qlib.data.dataset.loader import QlibDataLoader
from qlib.data.dataset.processor import ZScoreNorm, Fillna
from qlib.utils import init_instance_by_config
from qlib.data.dataset import DatasetH, TSDatasetH, DataHandlerLP


def load_dataset(data_handler, segments):
    ds = DatasetH(data_handler, segments=segments)
    return ds


def load_data_handler(instruments, start_time, end_time, infer_processors, data_loader):
    dh = DataHandlerLP(instruments=instruments, start_time=start_time, end_time=end_time,
                       infer_processors=infer_processors,
                       data_loader=data_loader)

    #dh = DataHandlerLP(instruments=['sh600519'], start_time='20170101', end_time='20191231',
    #                   infer_processors=[Fillna()],
    #                   data_loader=data_loader)
    return dh


def load_data_loader(features, label=(["Ref($close, -2)/Ref($close, -1) - 1"], ["LABEL0"])):
    feature_tuple = features
    label_tuple = label
    qdl = QlibDataLoader(config={
        'feature': feature_tuple,
        'label': label_tuple,
    })
    return qdl


class DataMgr:

    def __init__(self):
        self.market = "csi300"
        self.benchmark = "SH000300"

    def load_dataset(self,
                     features,
                     instruments='csi300',
                     start_time='2010-01-01',
                     end_time='2022-08-01',
                     segments={
                         "train": ("2010-01-01", "2014-12-31"),
                         "valid": ("2015-01-01", "2016-12-31"),
                         "test": ("2017-01-01", "2022-12-01"),
                     }):
        qdl = load_data_loader(features=features)
        print(qdl)
        dh = load_data_handler(instruments, start_time, end_time, [Fillna()], qdl)
        print(dh)
        ds = load_dataset(dh, segments)
        return ds

    def init_dataset(self, config=None):
        data_handler_config = {
            "start_time": "2008-01-01",
            "end_time": "2020-08-01",
            "fit_start_time": "2008-01-01",
            "fit_end_time": "2014-12-31",
            "instruments": self.market,
        }

        config = {
            "class": "DatasetH",
            "module_path": "qlib.data.dataset",
            "kwargs": {
                "handler": {
                    "class": "Alpha158",
                    "module_path": "qlib.contrib.data.handler",
                    "kwargs": data_handler_config,
                },
                "segments": {
                    "train": ("2008-01-01", "2014-12-31"),
                    "valid": ("2015-01-01", "2016-12-31"),
                    "test": ("2017-01-01", "2020-08-01"),
                },
            },
        }
        ds = init_instance_by_config(config)
        return ds

    def init_model(self, config=None):
        config = {
            "class": "LGBModel",
            "module_path": "qlib.contrib.model.gbdt",
            "kwargs": {
                "loss": "mse",
                "colsample_bytree": 0.8879,
                "learning_rate": 0.0421,
                "subsample": 0.8789,
                "lambda_l1": 205.6999,
                "lambda_l2": 580.9768,
                "max_depth": 8,
                "num_leaves": 210,
                "num_threads": 20,
            },
        }
        model = init_instance_by_config(config)
        return model


if __name__ == '__main__':
    pass
